﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using binary.Classes;

namespace binary.Helpers
{
    public static class AutodateHelper
    {
        public static Autodate GetDate()
        {
            const string filename = "autodate.xml";
            string text="";
            Autodate autodate = new Autodate();

            XmlDocument d = new XmlDocument();
            if (File.Exists(filename))
            {
                d.Load(filename);
                XmlNodeList nodeList = d.SelectNodes("autodate");
                foreach (XmlNode node in nodeList)
                {
                    if (node["location"] != null)
                        autodate.Location = node["location"].InnerText;
                    if (node["directory"] != null)
                        autodate.Directory = node["directory"].InnerText;
                }
            }
            return autodate;
        }


        public static bool IncrementDate()
        {
            try
            {
                string file = "autodate.xml";
                DateTime date = DateTime.ParseExact(AutodateHelper.GetDate().Directory, "yyyyMMdd", null);
                if (File.Exists(file))
                {
                    string content = File.ReadAllText(file);
                    content = content.Replace(string.Format("{0:yyyyMMdd}", date), string.Format("{0:yyyyMMdd}", date.AddDays(1)));
                    File.WriteAllText(file,content);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
